﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PokeData;

namespace PokeTD_Datatool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        WavesDS wds = new WavesDS();
        string openedFile = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            dgvCreeps.DataSource = wds.Creeps;
            dgvTower.DataSource = wds.Towers;
            dgvWaves.DataSource = wds.Waves;
        }

        private void xMLLadenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "PokeTD XML-Dateien|*.xml";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    wds.Clear();
                    wds.ReadXml(ofd.FileName);
                    openedFile = ofd.FileName;
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Fehler beim Laden der Datei " + ofd.FileName + ":\r\n\r\n" + ex.Message,"Fehler",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }
        }

        private void xMLSpeichernToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openedFile == String.Empty)
            {
                xMLSpeichernUnterToolStripMenuItem_Click(sender, e);
            }
            else
            {
                try
                {
                    wds.WriteXml(openedFile);
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Fehler beim Speichern der Datei " + openedFile + ":\r\n\r\n" + ex.Message,"Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void xMLSpeichernUnterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "PokeTD XML-Dateien|*.xml";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    wds.WriteXml(sfd.FileName);
                    openedFile = sfd.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Fehler beim Speichern der Datei " + sfd.FileName + ":\r\n\r\n" + ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void neuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("Ungespeicherte Änderungen verwerfen?", "PokeTD-Datatool", MessageBoxButtons.YesNoCancel);
            if (r == DialogResult.Cancel) return;
            if (r == DialogResult.No) xMLSpeichernToolStripMenuItem_Click(sender, e);
            wds.Clear();
            openedFile = "";
        }

    }
}
